package com.stox.ui.swing.view;

import java.awt.BorderLayout;
import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;

import com.stox.client.CountryClient;
import com.stox.client.ExchangeClient;
import com.stox.client.ScripClient;
import com.stox.model.Country;
import com.stox.model.Exchange;
import com.stox.model.Scrip;
import com.stox.ui.swing.event.NavigatorTreeMouseAdapter;
import com.stox.ui.swing.util.MessageSource;
import com.stox.ui.swing.widget.AutoNavigationPanel;
import com.stox.ui.swing.widget.wrapper.JTree;

@org.springframework.stereotype.Component
public class ScripsPanel extends AbstractNavigatorTab {
	private static final long serialVersionUID = -2991903722464340263L;

	@Autowired
	private CountryClient countryClient;
	
	@Autowired
	private ExchangeClient exchangeClient;
	
	@Autowired
	private ScripClient scripClient;
	
	@Autowired
	private ApplicationEventPublisher applicationEventPublisher;
	
	@Autowired
	private NavigatorTreeMouseAdapter navigatorTreeMouseAdapter;
	
	private DefaultMutableTreeNode  root = new DefaultMutableTreeNode(MessageSource.getMessage("Countries"));
	
	private JTree tree = new JTree(root);
	
	@Override
	protected JTree getJTree() {
		return tree;
	}
	
	@Override
	public int getOrder() {
		return 0;
	}
	
	@Override
	public String getName() {
		return MessageSource.getMessage("Scrips");
	}
	
	public void initializeInternal(){
		tree.addMouseListener(navigatorTreeMouseAdapter);

		JScrollPane scrollPane = new JScrollPane(tree);
	
		List<Country> countries = countryClient.findAll();
		if(null != countries && !countries.isEmpty()){
			for(Country country : countries){
				root.add(createCountryNode(country));
			}
		}
		
		add(new AutoNavigationPanel(tree, applicationEventPublisher), BorderLayout.NORTH);
		
		add(scrollPane, BorderLayout.CENTER);
		DefaultTreeModel treeModel = (DefaultTreeModel)tree.getModel();
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)treeModel.getRoot();
		DefaultMutableTreeNode targetNode = (DefaultMutableTreeNode)node.children().nextElement();
		TreePath path = new TreePath(treeModel.getPathToRoot(targetNode));
		tree.expandPath(path);
		tree.scrollPathToVisible(path);
		tree.setRootVisible(false);
	}
	
	private DefaultMutableTreeNode createCountryNode(Country country){
		DefaultMutableTreeNode countryNode = new DefaultMutableTreeNode(country);
		
		List<Exchange> exchanges = exchangeClient.findByCountryCode(country.getCode());
		if(null != exchanges && !exchanges.isEmpty()){
			for(Exchange exchange : exchanges){
				countryNode.add(createExchangeNode(exchange));
			}
		}
		
		return countryNode;
	}
	
	private DefaultMutableTreeNode createExchangeNode(Exchange exchange){
		DefaultMutableTreeNode exchangeNode = new DefaultMutableTreeNode(exchange);
		
		DefaultMutableTreeNode allNode = new DefaultMutableTreeNode(MessageSource.getMessage("All"));
		exchangeNode.add(allNode);
		
		DefaultMutableTreeNode indexNode = new DefaultMutableTreeNode(MessageSource.getMessage("Indices"));
		exchangeNode.add(indexNode);
		
		DefaultMutableTreeNode sectorNode = new DefaultMutableTreeNode(MessageSource.getMessage("Sectors"));
		exchangeNode.add(sectorNode);
	
		List<Scrip> scrips = scripClient.findByExchangeCode(exchange.getCode());
		if(scrips != null && !scrips.isEmpty()){
			for(Scrip scrip : scrips){
				allNode.add(createScripNode(scrip));
			}
		}
		return exchangeNode;
	}
	
	
	private DefaultMutableTreeNode createScripNode(Scrip scrip){
		return new DefaultMutableTreeNode(scrip);
	}
	
}
